<%--
  Created by IntelliJ IDEA.
  User: A
  Date: 2024/4/26
  Time: 15:33
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
    <link rel="stylesheet" href="../../static/layui/css/layui.css">
    <script src="../../static/jquery-2.1.4.js" type="text/javascript" charset="utf-8"></script>
    <script src="../../static/layui/layui.js" type="text/javascript" charset="utf-8"></script>
    <script src="../../static/mylayer.js" type="text/javascript" charset="utf-8"></script>
    <style>
        .ok{
            color: green;
        }
        .error{
            color: red;
        }
    </style>
</head>
<body>
    <%--修改form表单--%>
    <form class="layui-form" lay-filter="updateFormFilter" action="">
        <br>
        <div class="layui-form-item">
            <input type="hidden" name="id">
            <label class="layui-form-label">用户名</label>
            <div class="layui-input-inline layui-input-wrap">
                <input type="text" name="username" id="username" lay-verify="required" placeholder="请输入" autocomplete="off" class="layui-input" onblur="checkUsername()">
            </div>
            <span id="usernameMsg"></span>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">账号</label>
            <div class="layui-input-inline layui-input-wrap">
                <input type="text" name="account" id="account" lay-verify="required" placeholder="请输入" autocomplete="off" class="layui-input" onblur="checkAccount()">
            </div>
            <span id="accountMsg"></span>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">密码</label>
            <div class="layui-input-inline layui-input-wrap">
                <input type="text" name="password" id="password" lay-verify="required" placeholder="请输入" autocomplete="off" class="layui-input" onblur="checkPassword()">
            </div>
            <span id="passwordMsg"></span>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">头像</label>
            <div class="layui-input-inline layui-input-wrap">
                <div style="width: 132px;">
                    <div class="layui-upload-list">
                        <img class="layui-upload-img" width="150px" height="150px" class="layui-upload-img" id="demo1">
                        <div id="demoText"></div>
                        <input type="hidden" name="avatar" id="imageId">
                    </div>
                </div>
                <button type="button" class="layui-btn" id="uploadId">
                    <i class="layui-icon layui-icon-upload"></i> 头像上传
                </button>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">性别</label>
            <div class="layui-input-block">
                <input type="radio" name="gender" value="男" title="男" checked>
                <input type="radio" name="gender" value="女" title="女">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">年龄</label>
            <div class="layui-input-inline layui-input-wrap">
                <input type="text" name="age" id="age" lay-verify="required" placeholder="请输入" autocomplete="off" class="layui-input" onblur="checkAge()">
            </div>
            <span id="ageMsg"></span>
        </div>
        <div class="layui-form-item">
            <div class="layui-inline">
                <label class="layui-form-label">邮箱</label>
                <div class="layui-input-inline">
                    <input type="text" name="email" id="email" lay-verify="required" placeholder="请输入" autocomplete="off" class="layui-input" onblur="checkEmail()">
                </div>
                <span id="emailMsg"></span>
            </div>
        </div>
        <div class="layui-form-item">
            <div class="layui-inline">
                <label class="layui-form-label">电话</label>
                <div class="layui-input-inline">
                    <input type="text" name="phone" id="phone" lay-verify="phone" placeholder="请输入" autocomplete="off" class="layui-input" onblur="checkPhone()">
                </div>
                <span id="phoneMsg"></span>
            </div>
        </div>
        <div class="layui-form-item">
            <div class="layui-input-block">
                <button type="submit" class="layui-btn" lay-submit lay-filter="submitUpdate">立即提交</button>
                <button type="reset" class="layui-btn layui-btn-primary">重置</button>
            </div>
        </div>
    </form>
    <script>
        function checkUsername () {
            var username = $('#username').val();
            var regex = /^[\u4e00-\u9fa5a-zA-Z0-9_]{1,10}$/;
            if (regex.test(username)) {
                $('#usernameMsg').html('该用户名格式正确');
                $('#usernameMsg').removeClass();
                $('#usernameMsg').addClass('ok');
            } else {
                $('#usernameMsg').html('1-10位的汉字、字母、数字、下划线');
                $('#usernameMsg').removeClass();
                $('#usernameMsg').addClass('error');
            }
        };
        function checkAccount () {
            var account = $('#account').val();
            var regex = /[a-zA-Z0-9_]{5,10}/;
            if (regex.test(account)) {
                $('#accountMsg').html('该账号格式正确');
                $('#accountMsg').removeClass();
                $('#accountMsg').addClass('ok');
            } else {
                $('#accountMsg').html('5-10位的字母、数字、下划线');
                $('#accountMsg').removeClass();
                $('#accountMsg').addClass('error');
            }
        };
        function checkPassword () {
            var password = $('#password').val();
            var regex = /^[\S]{6,16}$/;
            if (regex.test(password)) {
                $('#passwordMsg').html('该密码格式正确');
                $('#passwordMsg').removeClass();
                $('#passwordMsg').addClass('ok');
            } else {
                $('#passwordMsg').html('密码必须为 6 到 16 位的非空字符');
                $('#passwordMsg').removeClass();
                $('#passwordMsg').addClass('error');
            }
        };
        function checkAge () {
            var age = $('#age').val();
            var regex = /^(?:[1-9][0-9]?|120)$/;
            if (regex.test(age)) {
                $('#ageMsg').html('该年龄格式正确');
                $('#ageMsg').removeClass();
                $('#ageMsg').addClass('ok');
            } else {
                $('#ageMsg').html('年龄必须为 1 到 120 之内的数字');
                $('#ageMsg').removeClass();
                $('#ageMsg').addClass('error');
            }
        };
        function checkPhone () {
            var phone = $('#phone').val();
            var regex = /^\d{7,15}$/;
            if (regex.test(phone)) {
                $('#phoneMsg').html('该电话号码格式正确');
                $('#phoneMsg').removeClass();
                $('#phoneMsg').addClass('ok');
            } else {
                $('#phoneMsg').html('电话号码必须为 7 到 15 位的非空数字');
                $('#phoneMsg').removeClass();
                $('#phoneMsg').addClass('error');
            }
        };
        function checkEmail () {
            var email = $('#email').val();
            var regex = /^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$/;
            if (regex.test(email)) {
                $('#emailMsg').html('该邮箱格式正确');
                $('#emailMsg').removeClass();
                $('#emailMsg').addClass('ok');
            } else {
                $('#emailMsg').html('邮箱格式错误');
                $('#emailMsg').removeClass();
                $('#emailMsg').addClass('error');
            }
        };
        layui.use(['form','upload'], function(){
            var form = layui.form;
            var upload = layui.upload;

            var uploadInst = upload.render({
                elem: '#uploadId',
                url: '/upload', // 实际使用时改成您自己的上传接口即可。
                before: function(obj){
                    // 预读本地文件示例，不支持ie8
                    obj.preview(function(index, file, result){
                        $('#demo1').attr('src', result); // 图片链接（base64）
                    });
                },
                done: function(result){
                    // 若上传失败
                    if(result.code == 0){
                        $('#imageId').val(result.data);
                        $('#demoText').html(''); //置空上传失败的状态
                    } else {
                        mylayer.errorMsg(result.msg);
                    }
                },
                error: function(){
                    // 演示失败状态，并实现重传
                    var demoText = $('#demoText');
                    demoText.html('<span style="color: #FF5722;">上传失败</span> <a class="layui-btn layui-btn-xs demo-reload">重试</a>');
                    demoText.find('.demo-reload').on('click', function(){
                        uploadInst.upload();
                    });
                },
            });
            var queryString = window.location.search;
            var urlParams = new URLSearchParams(queryString);
            var id = urlParams.get("id");
            $.post(
                '/user?method=selectById',
                {'id': id},
                function (result) {
                    if (result.code == 0) {
                        form.render('select');
                        form.val('updateFormFilter', result.data);
                        $('#demo1').attr('src', '/pic/' + result.data.avatar);
                    }
                },
                'json'
            );
            form.on('submit(submitUpdate)',function (data) {
                var field = data.field;
                console.log(data);
                $.post(
                    '/user?method=update',
                    field,
                    function (result) {
                        console.log(result);
                        if (result.code == 0) {
                            mylayer.okMsg(result.msg);
                            // layer.close(index);
                            // table.reload('tableId');
                            setInterval(function() {
                                //关闭弹出框
                                var index = parent.layer.getFrameIndex(window.name);
                                parent.layer.close(index);
                                //刷新父页面
                                window.parent.location.reload();
                            }, 2000)
                        } else {
                            mylayer.errorMsg(result.msg);
                        }
                    },
                    'json'
                );
                return false;
            });
        });
    </script>
</body>
</html>
